<?php
include($_SERVER["DOCUMENT_ROOT"]."/bob.php");
include($_SERVER["DOCUMENT_ROOT"]."/fonctions_globales.php");
$mysqli = new_mysqli($host, $user, $password, $database); //connection à la DB

if(get_data_in_db($mysqli, "gens", "user_id", $_SESSION['id'], "user_level")!='1'){
	header("Location: /"); exit();}
	
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="fr" dir="ltr"><head>
<meta charset="utf-8">
<title>Frum Daz Dingo</title>
<link rel="stylesheet" type="text/css" href="fourum.css">
</head>
<?php main_body($mysqli); ?><p align='center'><div class='boite'>
<a href='../index.php'>Index du fourum</a></div> <div class='boite'><a href='forums.php'>forums</a> 
</div> <div class='boite'><a href='smileys.php'>Smileys</a> 
</div> <div class='boite'><a href='rangs.php'>Rangs</a></div> <div class='boite'><a href='gens.php'>Permissions/ban</a> </div></p>

<?php
$id_smil = get_num(@$_GET['id']);

if(@$_POST['boutton']=="New"){
	$code = $mysqli->real_escape_string(get_data_ss_tag($_POST['cod']));
	$ima = $mysqli->real_escape_string(get_data_ss_tag($_POST['ima']));
	mysqli_query_with_error($mysqli, "SELECT * FROM forum_smilies WHERE code='$code' LIMIT 1"); 
	if($result->num_rows > 0)
		{echo "Ce code est déjà pris"; $_GET['mod']="new";}
	else 
		mysqli_query_with_error($mysqli, "INSERT INTO forum_smilies(code, smile_url) VALUES('$code', '$ima')");
}

if(@$_POST['boutton']=="Edit") {
	$code = $mysqli->real_escape_string(get_data_ss_tag($_POST['cod']));
	$id_smil = get_num($_POST['idd']);
	mysqli_query_with_error($mysqli, "SELECT * FROM forum_smilies WHERE code='$code' LIMIT 1"); 
	if($result->num_rows > 0)
		{echo "Ce code est déjà pris"; $_GET['mod']="edit";}
	else 
		mysqli_query_with_error($mysqli, "UPDATE forum_smilies SET code='$code' WHERE smilies_id='$id_smil'");
}

if(@$_GET['mod']=="supr"){
	mysqli_query_with_error($mysqli, "DELETE FROM forum_smilies WHERE smilies_id='$id_smil'");
}

if(@$_GET['mod']=="new"){
	 echo "<br><br><form action='smileys.php' method='post'><p align='center'>Code: <input type='text' class='noir'
	 name='cod'><br><br>img: <select name='ima' class='noir'>";
	 //recup des smileys
	$repert="../images/smiles";
	$doss = opendir($repert);
	$tsmileys = array();
	$i=0;
	while($file = readdir($doss) ){
	  if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $file) )   {
		$tsmileys[$i] = $file;
		$i++;
	   }
	}
	closedir($doss);
	 for($i=0; $i<sizeof($tsmileys); $i++) {
		$smil = get_data_ss_tag($tsmileys[$i]);
		echo"<option value='$smil'>$smil";
	 }
	 echo"</select><br><br><input type='submit' name='boutton' value='New'></form></p>";
}

if(@$_GET['mod']=="edit"){
 echo "<br><br><form action='smileys.php' method='post'><p align='center'>Code: <input type='text' class='noir'
 name='cod' value='".get_data_in_db($mysqli, "forum_smilies", "smilies_id", $id_smil, "code")."'><br><br><input type='submit' name='boutton' value='Edit'>
<input type='hidden' name='idd' value='$id_smil'></form></p>";
}

echo"<br><br><a href='smileys.php?mod=new' style='border:3px ridge blue;'>New</a><br><br>
<table border='1' style='text-align:center;'><tr><td>Code</td><td>Image</td><td>Editer
</td><td>Supprimer</td></tr>";
$result =  mysqli_query_with_error($mysqli, "SELECT * FROM forum_smilies ORDER BY smilies_id");
while($ligne = $result->fetch_assoc()) {
	$code = get_data_ss_tag($ligne['code']);
	$url = get_data_ss_tag($ligne['smile_url']);
	 echo"<tr><td>$code</td><td><img src='../images/smiles/$url'></td><td><a href='
	smileys.php?mod=edit&id={$ligne['smilies_id']}'>Editer</a></td><td><a href='smileys.php?mod=supr&id={$ligne['smilies_id']}' 
	onclick='return confirm(\"Veux-tu vraiment atomiser ce smiley innocent?\");'>Supprimer</a></td></tr>";
}

?>

</body></html>